package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;//importing layer package
/**ControlLayer package sets the location for AddressController class.
 * 
 * Usually a 'Controller' is the interface between a user interface component
 * and a model (e.g. AddressBook). Controllers should be thin classes, doing little
 * more than mapping user interface events to model functions.
 * 
 * The ModelLayer package is imported, because controllers must be able to access 
 * classes in the ModelLayer - AddressBook, DVDRegister and LoanRegister.
 * ---------------------------------------------------------------------------------
 * AddressController class is devided in 3 sections. In each section(except the first
 * one) methods with similar functionalities are placed. 
 *  
 *  Section Contents:
 *  Variables
 *  Constructors
 *      - AddressController()
 *  Methods
 *      - getPerson()
 *	    - printPerson()
 *      - createPerson()
 *      - deletePerson()
 *      - updatePerson()
 *      - listAllPersons()
 */

public class AddressController
{
  /** VARIABLES */
    //AddressController has to store the processed information somewhere. For the 
    //purpose to provide a container for the controller, the following object
    //reference(association) is needed.
    private  AddressBook addressBook; // Reference to AddressBook
  
  /** CONSTRUCTORS */
  public AddressController()
  {
      addressBook = AddressBook.getInstance();
      //Sets the container for storing items.
  }
  
  /** METHODS */
  public Person getPerson(String id)
  //Finds a person by exact id.
  {
      return addressBook.findPerson(id);
  }
  
  public String printPerson(String id)
  //Prints a person by exact id.
  {
      return addressBook.findPerson(id).toString();
  }
  
  public void createPerson(String id, String name, String address, String postalCode,
                            String city, String phone)
  //Creates a Person and store it in the AddressBook.
  {
      if(getPerson(id)!=null)
      //Prevents from adding persons with the same id.
      {
          System.out.println("Person with ID: "+id+" already exists.");
      }
      else
      {
          addressBook.addPerson(id, name, address, postalCode, city, phone); 
      }
    }
  
  public void deletePerson(String id)
  //Deletes a person by exact id.
  {
      addressBook.deletePerson(id);
  }
  
  public void updatePerson(String id, String name, String address, String postalCode,
                            String city, String phone)
  //Updates a person by exact id,
  {
      addressBook.updatePerson(getPerson(id),  name,  address,  postalCode,  city,  phone );
  }
  
  public ArrayList<Person> listAllPersons()
  //Shows all person in the AddressBook.
    {
         if(addressBook.getPerson().isEmpty()){
               System.out.println("Address book is empty.");
         }
          return addressBook.getPerson();
  }
}
